package com.it;

import com.it.bean.R;
import com.it.bean.RestStatusEnum;
import com.it.pojo.Article;
import com.it.pojo.params.article.ArticleQuery;
import com.it.pojo.vo.article.ArticlePlateVO;
import com.it.pojo.vo.article.ArticleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("article")
@Api(value = "v1", tags = "文章相关接口")

public class ArticleController {

    @Resource
    private ArticleService articleService;

    @PostMapping("")
    @ApiOperation(value = "保存文章", notes = "保存文章")
    public R<?> save(@RequestBody ArticleQuery articleQuery) {
        if (articleQuery == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        Article article = new Article();
        BeanUtils.copyProperties(articleQuery, article);
        Integer articleId = articleService.saveArticle(article);
        return articleId > 0 ? R.success("成功", articleId) : R.error("失败", 0);
    }

    @GetMapping("{articleid}")
    @ApiOperation(value = "根据文章id查询文章详情信息", notes = "根据文章id查询文章详情信息")
    public R<?> getArticle(@PathVariable("articleid") Integer articleId) {
        if (articleId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        Map<String, Object> map = articleService.getArticletVO(articleId);
        return R.success("成功", map);
    }

    @GetMapping("list")
    @ApiOperation(value = "根据板块id获取相关帖子信息", notes = "根据板块id获取相关帖子信息")
    public R<?> getArticleVOByPlateId(@RequestParam("plateId") Integer plateId) {
        if (plateId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        List<ArticlePlateVO> list = articleService.getArticleVOByPlateId(plateId);
        return R.success("成功", list);
    }

    @GetMapping("index")
    @ApiOperation(value = "获取首页的相关文章信息", notes = "获取首页的相关文章信息")
    public R<?> listIndex(@RequestParam("pageNum") Integer pageNum, @RequestParam("limit") Integer limit) {
        if (pageNum <= 0 || limit <= 0) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        Map<String, Object> map = articleService.listArticleIndexVO(pageNum, limit);
        return R.success("成功", map);
    }


}
